package com.jky.trial.controller;

import com.jky.trial.common.BaseResult;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.jwt.JWTUtil;
import com.jky.trial.entity.Record;
import com.jky.trial.entity.TaskSample;
import com.jky.trial.entity.User;
import com.jky.trial.service.ExportService;
import com.jky.trial.service.TaskSampleService;
import com.jky.trial.vo.TaskSampleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/api/v1/sample")
public class TaskSampleController {

    private TaskSampleService recordService;
    private ExportService exportService;

    @Autowired
    TaskSampleController(TaskSampleService recordService, ExportService exportService) {
        this.recordService = recordService;
        this.exportService = exportService;
    }

    @PostMapping
    public BaseResult create(@RequestBody TaskSample record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        record.setCreatorId(user.getId());//

        recordService.create(record);
        return new BaseResult(true, record.getId());
    }

    @PutMapping
    public BaseResult update(@RequestBody TaskSample record) {
        return new BaseResult(true, recordService.update(record));
    }
    @PutMapping("/send")
    public BaseResult send(@RequestBody TaskSample record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        return new BaseResult(true, recordService.send(record,user));
    }

    /**
     * 确认土样，单个或多个
     *  record.sampleIds 土样Id，
     * @param record
     * @param request
     * @return
     */
    @PutMapping("/confirm")
    public BaseResult confirm(@RequestBody TaskSampleVo record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        return new BaseResult(true, recordService.confirm(record,user));
    }

    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    /**
     * 批量导入勘察点
     *
     * @param file      excel文件
     * @param projectId 项目ID
     * @param request
     * @param response
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public BaseResult importData(@RequestParam("file") MultipartFile file,
                                 Integer projectId,//项目ID
                                 HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        return null;
    }

    @GetMapping("/{id}")
    public BaseResult get(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @GetMapping("/moreDetail/{id}")
    public BaseResult moreDetail(@PathVariable Integer id) {
        return new BaseResult(true, recordService.moreDetail(id));
    }

    /**
     * 项目土样
     *
     * @param record
     * @return
     */
    @GetMapping("/project/{projectId}")
    public ListResult page(TaskSampleVo record) {
        return recordService.page(record);
    }

    /**
     * 任务土样
     *
     * @param record
     * @return
     */
    @GetMapping("/task/{taskId}")
    public ListResult taskPage(TaskSampleVo record) {
        return recordService.page(record);
    }

    /**
     * 确认单包含土样
     * @param billId
     * @return
     */
    @GetMapping("/bill/{billId}")
    public ListResult sampleList(@PathVariable Integer billId) {
        return recordService.billSample(billId);
    }
    @GetMapping("/book/{bookId}")
    public ListResult bookSampleList(@PathVariable Integer bookId) {
        return recordService.bookSample(bookId);
    }

    /**
     * 工序土样
     *
     * @param record
     * @return
     */
    @GetMapping("/process/{processId}")
    public ListResult pageByProcessId(TaskSampleVo record) {
        return recordService.page(record);
    }

    @GetMapping("/list")
    public ListResult list(TaskSampleVo record) {
        return new ListResult(true, recordService.list(record));
    }
}
